const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    userInfo: {}, // 用户信息
    orderCount: {}, // 订单数量
    showBindPhoneModal : false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin()
    });
    // 获取当前用户信息
    _this.getUserDetail();
    // 更新购物车角标
    App.setCartTabBadge()
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '您家楼下的洗衣洗鞋店',
      path: '/pages/index/index?referee_id='+this.data.userInfo.user_id,
      imageUrl: '/images/logo.jpg',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function (result) {
      _this.setData(result.data);
      // 验证是否绑定了手机号
      if(result.data.userInfo.phone == ''){
        _this.setData({
          showBindPhoneModal : true
        })
      }
    });
  },
  bindUserPhone(e){
    let _this = this;
    if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
      wx.showToast({
        title: '您选择了拒绝，无法继续使用后续服务',
        icon: 'none'
      })
      return false;
    }
    var params = {
      code: e.detail.code,
      type:'phone'
    };
    _this.setData({
      loading: true
    })
    //已有所有需要的参数，直接请求
    App._post_form('user/bindPhone',params,function(res) {
      // 登录成功
      if (res.code == 1) {
        wx.showToast({
          title: '绑定成功',
          icon:'success'
        })
      }
    },function(err) {
      console.log(err,'err==')
      wx.showToast({
        title: '绑定失败',
        icon: 'error'
      })
    })
  },
  /**
   * 订单导航跳转
   */
  onTargetOrder(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    let urls = {
      all: '/pages/order/index?type=all',
      payment: '/pages/order/index?type=payment',
      received: '/pages/order/index?type=received',
      refund: '/pages/order/refund/index',
    };
    // 转跳指定的页面
    wx.navigateTo({
      url: urls[e.currentTarget.dataset.type]
    })
  },

  /**
   * 菜单列表导航跳转
   */
  onTargetMenus(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: '/' + e.currentTarget.dataset.url
    })
  },

  /**
   * 跳转我的钱包页面
   */
  onTargetWallet(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: './wallet/index'
    })
  },

  /**
   * 跳转积分明细页
   */
  onTargetPoints(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    wx.navigateTo({
      url: '../points/log/index'
    });
  },

  /**
   * 跳转到登录页
   */
  onLogin() {
    // wx.navigateTo({
    //   url: '../login/login',
    // });
    App.doLogin();
  },

  /**
   * 验证是否已登录
   */
  onCheckLogin() {
    let _this = this;
    if (!_this.data.isLogin) {
      App.showError('很抱歉，您还没有登录');
      return false;
    }
    return true;
  },
  /**
   * 跳转到登录页
   */
  onInfo() {
    wx.navigateTo({
      url: 'info/index',
    });
  },
  /**
   * 配送员扫描收件
   */
  onDeliveryScan(event){
    var _this = this;
    let type = event.currentTarget.dataset.type;
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res)
        if (res.errMsg == "scanCode:ok") {
          let path = res.path;
          let did = 0
          // 兼容生成的二维码参数中带scene
          if(path.indexOf('scene') == -1){
            let params = path.split('=');
            did = params[1];
          }else{
            path = decodeURIComponent(path);
            let params = path.split('=');
            did = params[1].split(':')[1];
          }
          // 判断二维码中的ID是否和订单的门店ID一致
          wx.navigateTo({
            url: 'delivery/index?shop_id='+did+'&type='+type,
          });
        }
      }
    })
  },
  onSwitchOrder(event){
    let url = event.currentTarget.dataset.url
    wx.switchTab({
      url: url,
    })
  },
  onSwitchAgent(event){
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  }
})